package edu.niu.mcaslab;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private ListView lstCategory;
    private ListView lstApplication;
    private ListView lstPermission;
    private TextView tabCategory;
    private TextView tabApplication;
    private TextView tabPermission;
    private enum VIEWS {Category, Application, Permission};
    private VIEWS currentView;
    private final int ACTIVITY_RESULT_PREFERENCE = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lstCategory = (ListView)findViewById(R.id.listviewcategory);
        lstCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                Intent intent = new Intent(getBaseContext() , CategoryDetail.class);
                intent.putExtra("categoryId",id);
                startActivity(intent);
            }
        });

        lstApplication = (ListView)findViewById(R.id.listviewapplication);
        lstApplication.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                Intent intent = new Intent(getBaseContext() , ApplicationDetail.class);
                intent.putExtra("applicationId",id);
                startActivity(intent);
            }
        });

        lstPermission = (ListView)findViewById(R.id.listviewpermission);
        lstPermission.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                Intent intent = new Intent(getBaseContext() , PermissionDetail.class);
                intent.putExtra("permissionId",id);
                startActivity(intent);
            }
        });

        tabCategory = (TextView)findViewById(R.id.tab_category);
        tabCategory.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (currentView != VIEWS.Category) {
                    switchTo(VIEWS.Category);
                }
            }
        });

        tabApplication = (TextView)findViewById(R.id.tab_application);
        tabApplication.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (currentView != VIEWS.Application) {
                    switchTo(VIEWS.Application);
                }
            }
        });

        tabPermission = (TextView)findViewById(R.id.tab_permission);
        tabPermission.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (currentView != VIEWS.Permission) {
                    switchTo(VIEWS.Permission);
                }
            }
        });

        switchTo(VIEWS.Category);

        Utilities.database = new AppPermissionDatabase(this);
        Utilities.database.isUpToDate();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void databaseUpdated() {
        refreshData();
    }

    private void refreshData() {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        boolean categoryOrder = pref.getBoolean("category_order", false);
        boolean applicationName = pref.getBoolean("application_name", true);
        boolean applicationOrder = pref.getBoolean("application_order", false);
        boolean permissionOrder = pref.getBoolean("permission_order", false);
        boolean hideSystemApp = pref.getBoolean("hide_system_app", false);

        String systemAppWhere = "";
        if (hideSystemApp) systemAppWhere = "WHERE system = 0 ";

        String nameField;
        nameField = (applicationName) ? "application.label" : "application.name";

        String orderField;
        if (applicationOrder)
            orderField = "name COLLATE NOCASE ASC";
        else
            orderField = "Count(relation_application_permission.permission) COLLATE NOCASE DESC";

        Cursor applicationCursor = Utilities.database.database.rawQuery("SELECT id AS _id, " + nameField + " || ' (' || Count(permission) || ')' AS name, application.name AS package " +
                "FROM application " +
                "LEFT OUTER JOIN relation_application_permission ON application.id = relation_application_permission.application " +
                systemAppWhere + "GROUP BY application.id ORDER BY " + orderField + ";", null);
        startManagingCursor(applicationCursor);

        List<AppListItem> items = new ArrayList<AppListItem>();

        PackageManager pm = getPackageManager();
        try {
            for (applicationCursor.moveToFirst(); !applicationCursor.isAfterLast(); applicationCursor.moveToNext()) {
                items.add(new AppListItem(applicationCursor.getLong(0), pm.getApplicationIcon(applicationCursor.getString(2)), applicationCursor.getString(1)));
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        AppListAdapter appAdapter = new AppListAdapter(this, items);
        lstApplication.setAdapter(appAdapter);

        if (permissionOrder)
            orderField = "permission.name COLLATE NOCASE ASC";
        else
            orderField = "Count(relation_application_permission.application) COLLATE NOCASE DESC";

        Cursor permissionCursor = Utilities.database.database.rawQuery("SELECT permission.id AS _id, permission.name || ' (' || Count(application) || ')' AS name " +
                "FROM permission " +
                "LEFT OUTER JOIN relation_application_permission ON permission.id = relation_application_permission.permission " +
                "LEFT OUTER JOIN application ON application.id = relation_application_permission.application " +
                systemAppWhere +
                "GROUP BY permission.id " +
                "HAVING Count(application) > 0 " +
                "ORDER BY " + orderField + ";", null);
        startManagingCursor(permissionCursor);
        ListAdapter permissionAdapter = new SimpleCursorAdapter(this, R.layout.permission_list_item, permissionCursor, new String[]{"name"}, new int[]{R.id.listviewpermissiontext});
        lstPermission.setAdapter(permissionAdapter);

        if (categoryOrder)
            orderField = "category.name COLLATE NOCASE ASC";
        else
            orderField = "Count(DISTINCT relation_application_permission.application) COLLATE NOCASE DESC";

        Cursor categoryCursor = Utilities.database.database.rawQuery("SELECT category.id AS _id, category.name || ' (' || Count(DISTINCT application) || ')' AS name " +
                "FROM category " +
                "LEFT OUTER JOIN relation_category_permission ON category.id = relation_category_permission.category " +
                "INNER JOIN relation_application_permission ON relation_category_permission.permission = relation_application_permission.permission " +
                "LEFT OUTER JOIN application ON application.id = relation_application_permission.application " +
                systemAppWhere +
                "GROUP BY category.id " +
                "HAVING Count(DISTINCT application) > 0 " +
                "ORDER BY " + orderField + ";", null);
        startManagingCursor(categoryCursor);
        ListAdapter categoryAdapter = new SimpleCursorAdapter(this, R.layout.category_list_item, categoryCursor, new String[]{"name"}, new int[]{R.id.listviewcategorytext});
        lstCategory.setAdapter(categoryAdapter);
    }

    public void isUpToDateResult(boolean upToDate) {
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case DialogInterface.BUTTON_POSITIVE:
                        Utilities.database.updateDatabase(MainActivity.this);
                        break;
                    case DialogInterface.BUTTON_NEGATIVE:
                        refreshData();
                        break;
                }
            }
        };

        if (upToDate) {
            refreshData();
        } else {
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setMessage(getString(R.string.alert_database_nottodate_text));
            alert.setPositiveButton(getString(R.string.alert_database_nottodate_yes), dialogClickListener);
            alert.setNegativeButton(getString(R.string.alert_database_nottodate_no), dialogClickListener);
            alert.show();
        }
    }
}
